package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicQueryObject;
import cn.wolfcode.luowowo.comment.repository.ScenicCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    //注入实现spring提供的repository对象
    private ScenicCommentMongoRepository repository;
    @Autowired
    private MongoTemplate template;
    @Reference
    private IScenicService scenicService;

    @Override
    public void save(ScenicComment comment) {
        //设置时间
        comment.setCreateTime(new Date());
        //判断是评论还是回复
        if (ScenicComment.TRAVEL_COMMENT_TYPE == comment.getType()) {
            //如果评论的评论
            //获取被回复的评论
            Optional<ScenicComment> op = repository.findById(comment.getId());
            //非空判断
            if (op.isPresent()) {
                ScenicComment scenicComment = op.get();
                //如果是评论的评论的回复
                if (comment.getRefCommentId() != null) {
                    //拿到该评论的list集合,找到id与RefCommentId相同的评论
                    List<ScenicComment> comments = scenicComment.getComments();
                    for (ScenicComment com : comments) {
                        if (com.getId().equals(comment.getRefCommentId())) {
                            //将评论信息设置进回复中
                            comment.setRefComment(com);
                        }
                    }
                }
                //生成一个不唯一的id
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                comment.setId(id);
                //将评论信息设置进该条回复的下面
                scenicComment.getComments().add(comment);
                //保存操作
                repository.save(scenicComment);
            }
        } else {
            //通过id查出景点
            Scenic scenic = scenicService.selectByPrimaryKey(comment.getScenicId());
            //给评论数加一
            scenic.setReplynum(scenic.getReplynum() + 1);
            //判断评论中是否含有图片
            if (comment.getCommentUrl() != "") {
                scenic.setHavePictureReplynum(scenic.getHavePictureReplynum() + 1);
            }
            //判断评论星数
            if (comment.getRank() < 3) {
                scenic.setBadReplynum(scenic.getBadReplynum() + 1);
            } else if (comment.getRank() == 3) {
                scenic.setCentreReplynum(scenic.getCentreReplynum() + 1);
            } else if (comment.getRank() > 3) {
                scenic.setGoodReplynum(scenic.getGoodReplynum() + 1);
            }
            //给mysql中更改热数据
            scenicService.saveOrUpdate(scenic);
            //保存操作
            repository.save(comment);
        }
    }

    @Override
    public Page findByScenicId(ScenicQueryObject qo) {
        Query query = new Query();
        if (qo.getScenicId() != -1) {
            query.addCriteria(Criteria.where("scenicId").is(qo.getScenicId()));
        }
        //总条数
        long count = template.count(query, ScenicComment.class);
        if (count == 0) {
            return Page.empty();
        }
        switch (qo.getType()) {
            case 1:
                query.addCriteria(Criteria.where("commentUrl").ne(""));
                break;
            case 2:
                query.addCriteria(Criteria.where("rank").gt(3));
                break;
            case 3:
                query.addCriteria(Criteria.where("rank").is(3));
                break;
            case 4:
                query.addCriteria(Criteria.where("rank").lt(3));
                break;
        }
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        query.with(pageRequest);
        List<ScenicComment> comments = template.find(query, ScenicComment.class);
        return new PageImpl(comments, pageRequest, count);
    }

    @Override
    public Optional<ScenicComment> findById(String id) {
        return repository.findById(id);
    }

    @Override
    public AjaxResult scenicThumbup(String id, Long userInfoId) {
        AjaxResult result = new AjaxResult();
        ScenicComment scenicComment = null;
        try {
            //通过该评论id查询出该条评论,并且给该评论加一
            Optional<ScenicComment> optional = repository.findById(id);
            //非空判断
            if (optional.isPresent()) {
                scenicComment = optional.get();
                //如果该条评论中点赞用户集合中有该用户,将该用户删除
                List<Long> list = scenicComment.getThumbuplist();
                if (list.contains(userInfoId)) {
                    result.mark("已经点过赞了");
                    list.remove(userInfoId);
                    //点赞数减一
                    scenicComment.setThumbupnum(scenicComment.getThumbupnum() - 1);
                } else {
                    list.add(userInfoId);
                    scenicComment.setThumbuplist(list);
                    //点赞数加一
                    scenicComment.setThumbupnum(scenicComment.getThumbupnum() + 1);
                }
            }
            //保存评论
            repository.save(scenicComment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有的点评
     *
     * @param userId
     * @return
     */
    public List<ScenicComment> findByUserId(Long userId) {
        List<ScenicComment> scenicComments = repository.findByuserId(userId);
        return scenicComments;
    }


}
